home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 24 / CU Amiga Magazine's Super CD-ROM 24 (1998)(EMAP Images)(GB)(Track 1 of 2)[!][issue 1998-07].iso / CUCD / Programming / Asap / Lines / LINES.C next >
Encoding:
C/C++ Source or Header  |  2006-10-09  |  3.3 KB  |  155 lines

  1. // Lines.c, a trivial example of using ASAP
  2.  
  3. // Adaptation of the SAS sample "lines.c"
  4.  
  5.  
  6.  
  7. //extern "C"
  8.  
  9. //{
  10. #include <stdlib.h>
  11. //}
  12.  
  13. #include <ASAP/Window.h>
  14. #include <ASAP/RastPort.h>
  15. #include <ASAP/MsgPort.h>
  16. #include <ASAP/IntuiMessage.h>
  17.  
  18. void DrawLines (ARastPort& , int, int);
  19.  
  20. void main(void)
  21. {
  22.  typedef ULONG MessageClass;
  23.  
  24.  NewWindow The_NewWindow =
  25.  {
  26.   100,100, 300,200,
  27.   2,1,                    /* detail, block pens */
  28.   IDCMP_CLOSEWINDOW | IDCMP_NEWSIZE | IDCMP_INTUITICKS,          /* IDCMP flags */
  29.   WINDOWDRAG | WINDOWCLOSE | GIMMEZEROZERO/* | WINDOWSIZING*/,   /* Window flags */
  30.   NULL,                   /* Pointer to first gadget */
  31.   NULL,                   /* Pointer to checkmark */
  32.   (unsigned char *) "ASAP Lines",          /* title */
  33.   NULL,                   /* screen pointer */
  34.   NULL,                   /* bitmap pointer */
  35.   50,50,640,400,          /* window not sized */
  36.   WBENCHSCREEN            /* type of screen */
  37.  };
  38.  
  39.  AWindow *pThe_Window = new (&The_NewWindow) AWindow;
  40.  
  41.  if (pThe_Window)
  42.  {
  43.   ARastPort& The_RastPort = *(ARastPort *) pThe_Window->RPort;                  /* Get the raster port pointer */
  44.  
  45.   The_RastPort.SetDrMd(JAM1);               /* Draw with foreground pen */
  46.  
  47.   int xlim = pThe_Window->Width,
  48.       ylim = pThe_Window->Height;
  49.  
  50.   MessageClass This_MessageClass = NULL;
  51.  
  52.   do
  53.   {
  54.    static
  55.    AMsgPort& The_MsgPort = * (AMsgPort *) pThe_Window->UserPort;
  56.  
  57.    The_MsgPort.WaitPort();
  58.  
  59.    static
  60.    AIntuiMessage *pThis_IntuiMessage;
  61.  
  62.    do
  63.    {
  64.     pThis_IntuiMessage = (AIntuiMessage *) The_MsgPort.GetMsg();
  65.  
  66.     if (pThis_IntuiMessage)
  67.     {
  68.      This_MessageClass = pThis_IntuiMessage->Class;
  69.  
  70.      pThis_IntuiMessage->ReplyMsg();
  71.  
  72.      switch (This_MessageClass)
  73.      {
  74.       case IDCMP_NEWSIZE: xlim = pThe_Window->Width; ylim = pThe_Window->Height; break;
  75.       case IDCMP_INTUITICKS: DrawLines(The_RastPort, xlim, ylim);
  76.      }
  77.     }
  78.    } while (pThis_IntuiMessage);
  79.   } while (This_MessageClass != IDCMP_CLOSEWINDOW);
  80.  
  81.   delete pThe_Window;
  82.  }
  83. }
  84.  
  85. void DrawLines (ARastPort& The_RastPort, int xlim, int ylim)
  86. {
  87.  register short i;
  88.  register int k;
  89.  
  90.  static
  91.  int iColour = rand() % 256;
  92.  
  93.  static
  94.  long j = 10;
  95.  
  96.  static
  97.  int x[2] = { rand() % xlim + 1, rand() % xlim + 1 },
  98.      y[2] = { rand() % ylim + 1, rand() % ylim + 1 },
  99.      xd[2] = { rand() % 10 + 1, rand() % 10 + 1  },
  100.      yd[2] = { rand() % 10 + 1, rand() % 10 + 1  },
  101.      ox[2][16],oy[2][16];
  102.  
  103.  The_RastPort.SetAPen(0);
  104.  The_RastPort.Move(ox[0][j & 15],oy[0][j & 15]);
  105.  The_RastPort.Draw(ox[1][j & 15],oy[1][j & 15]);
  106.  
  107.  The_RastPort.SetAPen(iColour);
  108.  The_RastPort.Move(x[0], y[0]);
  109.  The_RastPort.Draw(x[1], y[1]);
  110.  
  111.  if((rand() & 127) < 2)
  112.  {
  113.   if( ++ iColour > 255) iColour = 1;
  114.  
  115.   The_RastPort.SetAPen(iColour);
  116.  }
  117.  
  118.  for (i = 0; i < 2; ++ i)
  119.  {
  120.   ox[i][j] = x[i];
  121.   oy[i][j] = y[i];
  122.   x[i] += xd[i];
  123.   y[i] += yd[i];
  124.  
  125.   if (x[i] < 2) { x[i] = 2; xd[i] = -xd[i]; }
  126.   else if (x[i] > xlim)
  127.   {
  128.    x[i] = xlim;
  129.    xd[i] = -xd[i];
  130.   }
  131.  
  132.   if (y[i] < 2) { y[i] = 2; yd[i] = -yd[i]; }
  133.   else if (y[i] > ylim) { y[i] = ylim; yd[i] = -yd[i]; }
  134.  
  135.   if (((rand() >> 5) & 127) < 2)
  136.   {
  137.    if (xd[i] < 1) k = 1; xd[i] = (rand() >> 5) & 7;
  138.  
  139.    if(k == 1) xd[i] = -xd[i]; k = 0;
  140.   }
  141.  
  142.   if(((rand() >> 5) & 255) < 50)
  143.   {
  144.    if(yd[i] < 1) k = 1; yd[i] = (rand() >> 5) & 7;
  145.  
  146.    if(k == 1) yd[i] = -yd[i];
  147.    k = 0;
  148.   }
  149.  }
  150.  
  151.  j = ++ j % 16; // + 10;
  152. }
  153.  
  154.  
  155.